#include "ssicloud_apptrip_report.h"

using namespace ssicloud;
ApptripReport::ApptripReport()
    : ItemObject<ApptripNode>(LQ_BASE_URL "/trip/dashboard"
                           , OBJECT_NAME_ApptripReport
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ApptripReport::requestAll(const char *start, const char *end)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                 "\"start\": \"%s\","
                                 "\"end\": \"%s\""
                                 "}", VIN_STRING, start, end);
    mUrl.appendBody(m_body);
    return obtain();
}

int ApptripReport::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgTrips, mItem.avgTrips);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, trips, mItem.trips);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, days, mItem.days);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, hours, mItem.hours);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, kmFuels, mItem.kmFuels);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, fuels, mItem.fuels);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, miles, mItem.miles);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgSpeed, mItem.avgSpeed);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, times, mItem.times);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgFuels, mItem.avgFuels);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, svgSpeed, mItem.svgSpeed);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, oilFuel, mItem.oilFuel);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, electricFuel, mItem.electricFuel);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, gasFuel, mItem.gasFuel);

        // profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void ApptripReport::profile()
{
    GEN_Printf(LOG_INFO, "---------行程分析-行程统计-------");
    GEN_Printf(LOG_INFO, "avgTrips: %s", mItem.avgTrips.c_str());
    GEN_Printf(LOG_INFO, "trips: %s", mItem.trips.c_str());
    GEN_Printf(LOG_INFO, "days: %s", mItem.days.c_str());
    GEN_Printf(LOG_INFO, "hours: %s", mItem.hours.c_str());
    GEN_Printf(LOG_INFO, "kmFuels: %s", mItem.kmFuels.c_str());
    GEN_Printf(LOG_INFO, "fuels: %s", mItem.fuels.c_str());
    GEN_Printf(LOG_INFO, "miles: %s", mItem.miles.c_str());
    GEN_Printf(LOG_INFO, "avgSpeed: %s", mItem.avgSpeed.c_str());
    GEN_Printf(LOG_INFO, "times: %s", mItem.times.c_str());
    GEN_Printf(LOG_INFO, "avgFuels: %s", mItem.avgFuels.c_str());
    GEN_Printf(LOG_INFO, "svgSpeed: %s", mItem.svgSpeed.c_str());
    GEN_Printf(LOG_INFO, "oilFuel: %s", mItem.oilFuel.c_str());
    GEN_Printf(LOG_INFO, "electricFuel: %s", mItem.electricFuel.c_str());
    GEN_Printf(LOG_INFO, "gasFuel: %s", mItem.gasFuel.c_str());
    GEN_Printf(LOG_INFO, "---------完成-------");
}

LocationTravel::LocationTravel()
    : ItemObject<LocationTravelNode>(LQ_BASE_URL "/app/location/travel2"
                           , OBJECT_NAME_LocationTravel
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool LocationTravel::requestAll(const char *start, const char *end, int curPage, int pageSize)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                     "\"startTime\": \"%s\","
                                     "\"endTime\": \"%s\","
                                     "\"curPage\": \"%d\","
                                     "\"pageSize\": \"%d\""
                                     "}", VIN_STRING, start, end, curPage, pageSize);
    mUrl.appendBody(m_body);
    return obtain();
}

int LocationTravel::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
//        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, avgTrips, mItem.avgTrips);
//        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, trips, mItem.trips);
//        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, days, mItem.days);

        // profile();
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

void LocationTravel::profile()
{

}

ApptripDistribution::ApptripDistribution()
    : ListObject<ApptripDistributionNode>(LQ_BASE_URL "/customerSingleVehicle/trip/distribution/throttle"
                           , OBJECT_NAME_ApptripDistribution
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}
bool ApptripDistribution::requestAll(const char *start, const char *end)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                 "\"start\": \"%s\","
                                 "\"end\": \"%s\""
                                 "}", VIN_STRING, start, end);
    mUrl.appendBody(m_body);
    return obtain();
}

void ApptripDistribution::profile()
{
    ListTable<ApptripDistributionNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------行程分析-油门踏板分布-------");

    GEN_Printf(LOG_INFO, "chassisNo: %s", m_chassisNo.c_str());
    GEN_Printf(LOG_INFO, "plateNo: %s", m_plateNo.c_str());
    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "标签:%s, 百分比:%s, 里程:%s", it->label.string(), it->percents.c_str(), it->mile.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ApptripDistribution::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    m_chassisNo.clear();
    m_plateNo.clear();
    if (dataJson)
    {
        cJSON *data = cJSON_GetObjectItem(dataJson, "data");
        if (data) {
            for (cJSON *item = data->child; NULL != item; item = item->next )
            {
                ApptripDistributionNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, percents, tmp.percents);
                JSON_VALUETRING_SWAP_BYTESTRING(item, label, tmp.label);
                JSON_VALUETRING_SWAP_BYTESTRING(item, mile, tmp.mile);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, chassisNo, m_chassisNo);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, plateNo, m_plateNo);
        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

ApptripDistributionRpm::ApptripDistributionRpm()
    : ListObject<ApptripDistributionRpmNode>(LQ_BASE_URL "/customerSingleVehicle/trip/distribution/rpm"
                           , OBJECT_NAME_ApptripDistributionRpm
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}
bool ApptripDistributionRpm::requestAll(const char *start, const char *end)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                 "\"start\": \"%s\","
                                 "\"end\": \"%s\""
                                 "}", VIN_STRING, start, end);
    mUrl.appendBody(m_body);
    return obtain();
}

void ApptripDistributionRpm::profile()
{
    GEN_Printf(LOG_INFO, "---------行程分析-转速分布-------");
    ListTable<ApptripDistributionNode>::iterator it = mNodes.begin();

    GEN_Printf(LOG_INFO, "chassisNo: %s", m_chassisNo.c_str());
    GEN_Printf(LOG_INFO, "plateNo: %s", m_plateNo.c_str());
    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "标签:%s, 百分比:%s, 里程:%s", it->label.string(), it->percents.c_str(), it->mile.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ApptripDistributionRpm::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    m_chassisNo.clear();
    m_plateNo.clear();
    if (dataJson)
    {
        cJSON *data = cJSON_GetObjectItem(dataJson, "data");
        if (data) {
            for (cJSON *item = data->child; NULL != item; item = item->next )
            {
                ApptripDistributionNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, percents, tmp.percents);
                JSON_VALUETRING_SWAP_BYTESTRING(item, label, tmp.label);
                JSON_VALUETRING_SWAP_BYTESTRING(item, mile, tmp.mile);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, chassisNo, m_chassisNo);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, plateNo, m_plateNo);
        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

ApptripDistributionSpeed::ApptripDistributionSpeed()
    : ListObject<ApptripDistributionRpmNode>(LQ_BASE_URL "/customerSingleVehicle/trip/distribution/speed"
                           , OBJECT_NAME_ApptripDistributionSpeed
                           , NetUrl::NET_HTTP_METHOD_POST)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ApptripDistributionSpeed::requestAll(const char *start, const char *end)
{
    snprintf(m_body, sizeof(m_body), "{\"vin\": \"%s\","
                                 "\"start\": \"%s\","
                                 "\"end\": \"%s\""
                                 "}", VIN_STRING, start, end);
    mUrl.appendBody(m_body);
    return obtain();
}

void ApptripDistributionSpeed::profile()
{
    GEN_Printf(LOG_INFO, "---------行程分析-速度分布-------");
    ListTable<ApptripDistributionNode>::iterator it = mNodes.begin();

    GEN_Printf(LOG_INFO, "chassisNo: %s", m_chassisNo.c_str());
    GEN_Printf(LOG_INFO, "plateNo: %s", m_plateNo.c_str());
    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "标签:%s, 百分比:%s, 里程:%s", it->label.string(), it->percents.c_str(), it->mile.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ApptripDistributionSpeed::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    m_chassisNo.clear();
    m_plateNo.clear();
    if (dataJson)
    {
        cJSON *data = cJSON_GetObjectItem(dataJson, "data");
        if (data) {
            for (cJSON *item = data->child; NULL != item; item = item->next )
            {
                ApptripDistributionNode tmp;

                JSON_VALUETRING_SWAP_BYTESTRING(item, percents, tmp.percents);
                JSON_VALUETRING_SWAP_BYTESTRING(item, label, tmp.label);
                JSON_VALUETRING_SWAP_BYTESTRING(item, mile, tmp.mile);

                // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
                mNodes.push_back(tmp);
            }
        }

        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, chassisNo, m_chassisNo);
        JSON_VALUETRING_SWAP_BYTESTRING(dataJson, plateNo, m_plateNo);
        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}
